import { Injectable } from '@nestjs/common';
import * as svgCaptcha from 'svg-captcha'

@Injectable()
export class CodeService {
  createCode(req,res,session) {
    const captcha = svgCaptcha.create({
      size: 4, //验证码几个字符
      fontSize: 50, //字体大小
      width: 100, //宽度
      height: 34, //高度
      background: '#cc9966' //背景颜色
    })
    session.code = captcha.text
    res.type('image/svg+xml')
    res.send(captcha.data);
  }

  verifyCode(body: {code:string}, session: Record<string, any>) {
    console.log(body, session.code);
    if (body.code.toLocaleLowerCase() === session.code.toLocaleLowerCase()) {
      return {code: body.code,sessionCode: session.code,msg:'验证通过'}
    }
    return {code: body.code,sessionCode: session.code,msg:'验证失败'}
  }
}
